local wezterm = require("wezterm")

-- 备用图标
-- local CMD_ICON = utf8.char(0xe62a)
-- local NU_ICON = utf8.char(0xe7a8)
-- local PS_ICON = utf8.char(0xe70f)
-- local ELV_ICON = utf8.char(0xfc6f)
-- local WSL_ICON = utf8.char(0xf83c)
-- local YORI_ICON = utf8.char(0xf1d4)
-- local NYA_ICON = utf8.char(0xf61a)
--
-- local VIM_ICON = utf8.char(0xe62b)
-- local PAGER_ICON = utf8.char(0xf718)
-- local FUZZY_ICON = utf8.char(0xf0b0)
-- local HOURGLASS_ICON = utf8.char(0xf252)
-- local SUNGLASS_ICON = utf8.char(0xf9df)
--
-- local PYTHON_ICON = utf8.char(0xf820)
-- local NODE_ICON = utf8.char(0xe74e)
-- local DENO_ICON = utf8.char(0xe628)
-- local LAMBDA_ICON = utf8.char(0xfb26)
--
-- local SOLID_LEFT_ARROW = utf8.char(0xe0ba)
-- local SOLID_LEFT_MOST = utf8.char(0x2588)
-- local SOLID_RIGHT_ARROW = utf8.char(0xe0bc)
-- local ADMIN_ICON = utf8.char(0xf49c)

-- 定义图标常量
local GLYPH_SEMI_CIRCLE_LEFT = "" -- utf8.char(0xe0b6)
local GLYPH_SEMI_CIRCLE_RIGHT = "" -- utf8.char(0xe0b4)
local GLYPH_CIRCLE = "" -- utf8.char(0xf111)
local GLYPH_ADMIN = "󰯫" -- utf8.char(0xfc7e)
local GLYPH_EDITOR = ""
local GLYPH_LAZYGIT = "󰊢"
local GLYPH_TERM = ""
local GLYPH_DOCKER = "󰡨"
local GLYPH_MDBOOK = ""

local M = {}

-- 初始化单元格列表
M.cells = {}

-- 定义颜色配置
---@class MColors
---@field default table<string, string> 默认状态的颜色配置
---@field is_active table<string, string> 当前激活的标签的颜色配置
---@field hover table<string, string> 鼠标悬停时的颜色配置
M.colors = {
	default = { bg = "#000000", fg = "#404040" },
	is_active = { bg = "#913c20", fg = "#0F2536" },
	hover = { bg = "#000000", fg = "#909090" },
}

-- 定义进程名称检查函数及其对应的图标
---@class CheckFunctions
---@field admin table 检查是否为管理员模式的函数配置
---@field editor table 检查是否为编辑器进程的函数配置
---@field lazygit table 检查是否为 LazyGit 进程的函数配置
---@field term table 检查是否为终端进程的函数配置
---@field docker table 检查是否为 Docker 相关进程的函数配置
---@field mdbook table 检查是否为 mdbook 进程的函数配置
local check_functions = {
	admin = { pattern = { "^Administrator: " }, icon = GLYPH_ADMIN },
	editor = { pattern = { "^hx", "^vim", "^vi" }, icon = GLYPH_EDITOR },
	lazygit = { pattern = { "^git", "^lazygit" }, icon = GLYPH_LAZYGIT },
	term = { pattern = { "^pwsh", "^zsh" }, icon = GLYPH_TERM },
	docker = { pattern = { "^docker", "^lazydocker" }, icon = GLYPH_DOCKER },
	mdbook = { pattern = { "^mdbook" }, icon = GLYPH_MDBOOK },
}

---从完整的进程路径中提取进程名称，并移除可执行文件扩展名（如 `.exe`）。
---@param s string 完整的进程路径字符串
---@return string 提取后的进程名称, nil
function M.set_process_name(s)
	return string.gsub(s, "(.*[/\\])(.*)", "%2"):gsub("%.exe$", "")
end

-- 设置标题
function M.set_title(process_name, static_title, active_title, max_width, inset)
	local title = static_title:len() > 0 and static_title or (process_name:len() > 0 and process_name or active_title)
	title = "|" .. title .. " ~ " .. " "
	if title:len() > max_width - (inset or 6) then
		title = wezterm.truncate_right(title, max_width - (inset or 6))
	end
	return title
end

-- 检查模式匹配
function M.check_pattern(p, patterns)
	for _, pattern in ipairs(patterns) do
		if p:match(pattern) then
			return true
		end
	end
	return false
end

-- 插入单元格内容
function M.push(bg, fg, attribute, text)
	table.insert(M.cells, { Background = { Color = bg } })
	table.insert(M.cells, { Foreground = { Color = fg } })
	table.insert(M.cells, { Attribute = attribute })
	table.insert(M.cells, { Text = text })
end

-- 设置颜色
function M.get_color(tab, hover)
	if tab.is_active then
		return M.colors.is_active.bg, M.colors.is_active.fg
	elseif hover then
		return M.colors.hover.bg, M.colors.hover.fg
	else
		return M.colors.default.bg, M.colors.default.fg
	end
end

-- 设置标题格式
function M.setup()
	wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
		M.cells = {}

		-- 获取颜色
		local bg, fg = M.get_color(tab, hover)

		-- 获取进程名称和标题
		local process_name = M.set_process_name(tab.active_pane.foreground_process_name)
		local title = M.set_title(process_name, tab.tab_title, tab.active_pane.title, max_width, 8)

		-- 检查是否有未读输出
		local has_unseen_output = false
		for _, pane in ipairs(tab.panes) do
			if pane.has_unseen_output then
				has_unseen_output = true
				break
			end
		end

		-- 检查特定模式并添加图标
		for _, check in pairs(check_functions) do
			if M.check_pattern(process_name, check.pattern) then
				M.push(bg, fg, { Intensity = "Bold" }, " " .. check.icon)
			end
		end

		-- 左半圆
		M.push(fg, bg, { Intensity = "Bold" }, GLYPH_SEMI_CIRCLE_LEFT)

		-- 标题
		M.push(bg, fg, { Intensity = "Bold" }, " " .. title)

		-- 未读输出提示
		if has_unseen_output then
			M.push(bg, "#606060", { Intensity = "Bold" }, " " .. GLYPH_CIRCLE)
		end

		-- 右侧填充
		M.push(bg, fg, { Intensity = "Bold" }, " ")

		-- 右半圆
		M.push(fg, bg, { Intensity = "Bold" }, GLYPH_SEMI_CIRCLE_RIGHT)

		return M.cells
	end)
end

return M
